<?php defined('LFNC_VERSION') or die('forbidden');


/**
* MLT Web Engine
* http://www.molotsoft.ru
*
* @package MLT_WE
* @subpackage Lucky_Funcs
* @author Ivan Chistyakov <ivan@molotsoft.ru>
* @version $Id: image.lfnc.php 49 2008-09-26 12:41:43Z molotsoft@gmail.com $
* @link $HeadURL: http://lucky-funcs.googlecode.com/svn/branches/version_1/funcs/image.lfnc.php $
*/


function image_wave_x($src_im, $l, $a, $bg_color = 0) {
	$sx = imagesx($src_im);
	$sy = imagesy($src_im);
	$dst_im = imagecreatetruecolor($sx, $sy);
	imagefill($dst_im, 0, 0, $bg_color);
	for ($x = 0; $x <= $sx - 1; $x ++) {
		for ($y = 0; $y <= $sy - 1; $y ++) {
			$src_x = $x;
			$dst_x = (int)round($x + sin($y / $l) * $a);
			imagesetpixel($dst_im, $dst_x, $y, imagecolorat($src_im, $src_x, $y));
		}
	}
	return $dst_im;
}

function image_wave_y($src_im, $l, $a, $bg_color = 0) {
	$sx = imagesx($src_im);
	$sy = imagesy($src_im);
	$dst_im = imagecreatetruecolor($sx, $sy);
	imagefill($dst_im, 0, 0, $bg_color);
	for ($y = 0; $y <= $sy - 1; $y ++) {
		for ($x = 0; $x <= $sx - 1; $x ++) {
			$src_y = $y;
			$dst_y = (int)round($y + sin($x / $l) * $a);
			imagesetpixel($dst_im, $x, $dst_y, imagecolorat($src_im, $x, $src_y));
		}
	}
	return $dst_im;
}

function image_wave_xy($src_im, $x_l, $x_a, $y_l, $y_a, $bg_color = 0) {
	$tmp_im = image_wave_x($src_im, $x_l, $x_a, $bg_color);
	$dst_im = image_wave_y($tmp_im, $y_l, $y_a, $bg_color);
	imagedestroy($tmp_im);
	return $dst_im;
}


function image_wave2_x($src_im, $l, $a) {
	$sx = imagesx($src_im);
	$sy = imagesy($src_im);
	$dst_im = imagecreatetruecolor($sx, $sy);
	for ($x = 0; $x <= $sx - 1; $x ++) {
		for ($y = 0; $y <= $sy - 1; $y ++) {
			$src_x = $x;
			$dst_x = (int)round($x + sin($y / $l) * $a);
			$dst_x = (int)round($src_x + ($dst_x - $src_x) * sin($x / ($sx - 1) * M_PI)); // insensibly
			$color = imagecolorat($src_im, $src_x, $y);
			imagesetpixel($dst_im, $dst_x, $y, $color);
			// fill empty pixels
			if ($dst_x !== $src_x) {
				$x2 = ($dst_x > $src_x) ? ($x - 1) : ($x + 1);
				$src_x2 = $x2;
				$dst_x2 = (int)round($x2 + sin($y / $l) * $a);
				$dst_x2 = (int)round($src_x2 + ($dst_x2 - $src_x2) * sin($x2 / ($sx - 1) * M_PI));
				for ($cur_x = min($dst_x, $dst_x2) + 1; $cur_x <= max($dst_x, $dst_x2) - 1; $cur_x ++) {
					imagesetpixel($dst_im, $cur_x, $y, $color);
				}
			}
		}
	}
	return $dst_im;
}

function image_wave2_y($src_im, $l, $a) {
	$sx = imagesx($src_im);
	$sy = imagesy($src_im);
	$dst_im = imagecreatetruecolor($sx, $sy);
	for ($y = 0; $y <= $sy - 1; $y ++) {
		for ($x = 0; $x <= $sx - 1; $x ++) {
			$src_y = $y;
			$dst_y = (int)round($y + sin($x / $l) * $a);
			$dst_y = (int)round($src_y + ($dst_y - $src_y) * sin($y / ($sy - 1) * M_PI)); // insensibly
			$color = imagecolorat($src_im, $x, $src_y);
			imagesetpixel($dst_im, $x, $dst_y, $color);
			// fill empty pixels
			if ($dst_y !== $src_y) {
				$y2 = ($dst_y > $src_y) ? ($y - 1) : ($y + 1);
				$src_y2 = $y2;
				$dst_y2 = (int)round($y2 + sin($x / $l) * $a);
				$dst_y2 = (int)round($src_y2 + ($dst_y2 - $src_y2) * sin($y2 / ($sy - 1) * M_PI));
				for ($cur_y = min($dst_y, $dst_y2) + 1; $cur_y <= max($dst_y, $dst_y2) - 1; $cur_y ++) {
					imagesetpixel($dst_im, $x, $cur_y, $color);
				}
			}
		}
	}
	return $dst_im;
}

function image_wave2_xy($src_im, $x_l, $x_a, $y_l, $y_a) {
	$tmp_im = image_wave2_x($src_im, $x_l, $x_a);
	$dst_im = image_wave2_y($tmp_im, $y_l, $y_a);
	imagedestroy($tmp_im);
	return $dst_im;
}


function image_swirl($src_im, $a, $factor = 1) {
	$sx = imagesx($src_im);
	$sy = imagesy($src_im);
	$x_l = ($sx - 1) / (2 * M_PI);
	$y_l = - ($sy - 1) / (2 * M_PI);
	if ($factor >= 2) {
		$tmp_im = image_wave2_xy($src_im, $x_l, $a, $y_l, $a);
		$dst_im = image_swirl($tmp_im, $a, $factor - 1); // recursion
		imagedestroy($tmp_im);
		return $dst_im;
	}
	return image_wave2_xy($src_im, $x_l, $a, $y_l, $a);
}


?>